package com.gitee.wsl.compose.draw.sketch.noise

import androidx.compose.runtime.Composable
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import com.gitee.wsl.compose.draw.sketch.k5
import com.gitee.wsl.math.random.random
import com.gitee.wsl.math.Noise.noise3D
import com.gitee.wsl.unit.angle.AngleUnit
import com.gitee.wsl.unit.angle.ext.cos
import com.gitee.wsl.unit.angle.ext.cosF
import com.gitee.wsl.unit.angle.ext.plus
import com.gitee.wsl.unit.angle.ext.sin
import com.gitee.wsl.unit.angle.ext.sinF
import com.gitee.wsl.unit.angle.toDegrees
import kotlin.math.pow
import kotlin.math.sqrt

@Composable
fun showBlackHole() = k5 {

    noLoop()
    //angleMode = AngleMode.DEGREES

    val circleNumbers = 200
    val circleGap = 0.01f
    var i = 0
    onDraw {
        it.apply {
            while (i <= circleNumbers) {
                val radius = (this.size.width / 10) + i * 0.05f
                val k = (0.5f..1f).random() * sqrt(1.0 * i / circleNumbers).toFloat()
                val noisiness = 400f * (1.0f * i / circleNumbers).pow(2)

                var theta: AngleUnit = 0f.toDegrees()
                var bx = 0f
                var by = 0f
                while (theta.degrees < 361f) {
                    val r1 = theta.cos()
                    val r2 = theta.sin()
                    val r = radius + noise3D(1.0 * k * r1, 1.0 * k * r2, 1.0 * i * circleGap).toFloat() * noisiness

                    val x = this.size.width / 2 + r * theta.cosF()
                    val y = this.size.height / 2 + r * theta.sinF()

                    if (bx == 0f && by == 0f) {
                        bx = x
                        by = y
                    }

                    drawLine(Color(0xfff8a614), Offset(bx, by), Offset(x, y), 0.4f)
                    bx = x
                    by = y
                    theta += 1.0f.toDegrees()
                }
                i++
            }
        }
    }
}
